package com.voice.server.controller.root;

import com.voice.server.entity.Function;
import com.voice.server.entity.Role;
import com.voice.server.entity.User;
import com.voice.server.input.UserQuery;
import com.voice.server.service.FunctionService;
import com.voice.server.service.RoleService;
import com.voice.server.service.UserService;
import com.wangjiacai.utils.output.ResultVO;
import com.wangjiacai.utils.utils.ResultVOUtil;
import com.wangjiacai.utils.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/*
 * @auther: wangjiacai
 *
 * @create: 2019-01-28 20:10:23
 */
@RestController
@RequestMapping("/root")
public class RootController {

    @Autowired
    private FunctionService functionService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    @GetMapping("/function/tree")
    public ResultVO getFunctionTree(){
        return ResultVOUtil.success(functionService.getTreeNode());
    }

    @GetMapping("/function/tree/map")
    public List<Map> getFunctionTreeMap(){
        return functionService.getFunctionTreeMap();
    }


    @GetMapping("/function/{functionId}")
    public ResultVO getFunctionByFunctionId(@PathVariable("functionId") String functionId){
        Function function=functionService.findById(functionId);
        return ResultVOUtil.success(function);
    }

    @DeleteMapping("/function/{functionId}")
    public ResultVO deleteFunctionByFunctionId(@PathVariable("functionId") String functionId){
        functionService.deleteById(functionId);
        return ResultVOUtil.success();
    }

    @PostMapping("/function")
    public ResultVO postFunction(@Valid  Function function, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        functionService.addFunction(function);
        return ResultVOUtil.success();
    }

    @PutMapping("/function")
    public ResultVO putFunction(@Valid  Function function, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        functionService.updateFunction(function);
        return ResultVOUtil.success();
    }

    @GetMapping("/role/num")
    public ResultVO getRoleListNum(Role role,Integer size){
        long totalNum=roleService.getRoleListNum(role);
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @GetMapping("/role")
    public ResultVO getRoleList (Role role, @PageableDefault(sort = "updateDateTime") Pageable pageable){
        List<Role> list=roleService.getRoleList(role,pageable);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/role/all")
    public ResultVO getAllRoleList (){
        List<Role> list=roleService.getAllRoleList();
        return ResultVOUtil.success(list);
    }

    @DeleteMapping("/role/{roleId}")
    public ResultVO deleteRoleByRoleId (@PathVariable("roleId")String roleId){
        roleService.deleteRole(roleId);
        return ResultVOUtil.success();
    }

    @PostMapping("/role")
    public ResultVO postRole (@Valid  Role role, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        roleService.addRole(role);
        return ResultVOUtil.success();
    }

    @PutMapping("/role/assignRoleFunction")
    public ResultVO putRole (String bhs,String roleId){
        roleService.assignRoleFunctionByBhs(bhs,roleId);
        return ResultVOUtil.success();
    }

    @GetMapping("/role/{roleId}")
    public ResultVO findBhListByRoleId(@PathVariable("roleId") String roleId){
        List<String> functionIds=functionService.findBhListByRoleId(roleId);
        if(functionIds==null) functionIds=new ArrayList<>();
        return ResultVOUtil.success(functionIds);
    }

    @GetMapping("/user/num")
    public ResultVO getUserListNum(UserQuery userQuery, Integer size){
        long totalNum=userService.getUserListNum(userQuery);
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @GetMapping("/user")
    public ResultVO getUserList (UserQuery userQuery,@PageableDefault(sort = "updateDateTime") Pageable pageable){
        List<User> list=userService.getUserList(userQuery,pageable);
        return ResultVOUtil.success(list);
    }

}
